package com.cug.campus_sh_market_server.entity.po;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.cug.campus_sh_market_server.entity.myenum.UserTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 用户
 * </p>
 *
 * @author sxt
 * @since 2022-03-22
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "User对象", description = "用户")
public class User extends BaseEntity implements UserDetails {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    @TableId(value = "id", type = IdType.AUTO)
    public Integer id;

    @ApiModelProperty(value = "学院id")
    private Integer collegeId;

    @ApiModelProperty(value = "用户昵称")
    @TableField("username")
    private String myUsername;

    @ApiModelProperty(value = "真实姓名")
    private String realName;

    @ApiModelProperty(value = "用户账号")
    private String account;

    @ApiModelProperty(value = "登录密码")
    private String password;

    @ApiModelProperty(value = "盐值")
    private String salt;

    @ApiModelProperty(value = "身份标识(学生0/老师1)")
    private Integer identity;

    @ApiModelProperty(value = "用户类型(-1: 游客, 0: 普通用户, 1: 管理员, 2: 超级管理员)")
    private Integer userType;

    @ApiModelProperty(value = "电话")
    private String tel;

    @TableField("QQ")
    @ApiModelProperty(value = "QQ")
    private String QQ;

    @ApiModelProperty(value = "wechat")
    private String wechat;

    @ApiModelProperty(value = "用户状态(0: 启用, 1: 禁用)")
    private Integer active;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();

        if (this.userType == null) {
            authorities.add(new SimpleGrantedAuthority(UserTypeEnum.EMPTY.getUserDes()));
            return authorities;
        }

        if (this.userType.equals(UserTypeEnum.SUPER_ADMIN.getTypeCode())) {
            authorities.add(new SimpleGrantedAuthority(UserTypeEnum.SUPER_ADMIN.getUserDes()));
        } else if (this.userType.equals(UserTypeEnum.ADMIN.getTypeCode())) {
            authorities.add(new SimpleGrantedAuthority(UserTypeEnum.ADMIN.getUserDes()));
        } else if (this.userType.equals(UserTypeEnum.USER.getTypeCode())) {
            authorities.add(new SimpleGrantedAuthority(UserTypeEnum.USER.getUserDes()));
        } else {
            authorities.add(new SimpleGrantedAuthority(UserTypeEnum.EMPTY.getUserDes()));
        }
        return authorities;
    }

    @Override
    public String getUsername() {
        return account;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

}
